package com.mark.spring.annotation.register.factory;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * @Description: FactoryBean引导类
 * @Author: Mark
 * @CreateDate: 2019/9/21 10:22
 * @Version: 3.00.001
 * @Copyright : 豆浆油条个人非正式工作室
 *
 * 使用{@link org.springframework.beans.factory.FactoryBean} 注册组件
 * 1：默认获取到的Bean是工厂Bean{@link FactoryBean#getObject()} 方法创建的Bean
 * 2：如果需要获取Bean本身，我们需要在Bean name加一个&符号
 *
 * @see FactoryBean
 * @see BeanFactory
 */
@ComponentScan(basePackages = {"com.mark.spring.annotation.register.factory"})
public class FactoryBeanBootstrap {

    public static void main(String[] args) {
        //加载配置，获取bean
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(FactoryBeanBootstrap.class);

        //打印配置类注入的stringFactoryBean的类型
        Object obj = applicationContext.getBean("stringFactoryBean");
        //class java.lang.String 而不是com.mark.spring.annotation.register.factory.StringFactoryBean
        System.out.println(obj.getClass());

        //怎么获取StringFactoryBeanBean本身呢？
        Object obj2 = applicationContext.getBean(BeanFactory.FACTORY_BEAN_PREFIX+"stringFactoryBean");
        //class com.mark.spring.annotation.register.factory.StringFactoryBean
        System.out.println(obj2.getClass());

        //关闭上下文
        applicationContext.close();
    }
}
